#include "game.h"


int x=0, y=0;

void menu()
{
  printf("*****************************\n");
  printf("**1.play             0.exit**\n");
  printf("*****************************\n");
  printf("please input your choose:");
}


void ShowBoard(int board[][COL],int row,int col)
{
    int i = 0;
    printf("  ");
    for(i = 1; i <= COL; i++)
    {
      printf("%3d",i);
    }
    printf("\n");
    for(i=1;i<=ROW;i++)
    {
      printf("%2d",i);
      int j = 0;
      for(j=1;j<=COL;j++)
      {
        if(board[i-1][j-1]==PLAYER1)  
          printf("%3c",'x');
        else if(board[i-1][j-1]==PLAYER2)
          printf("%3c",'a');
        else
          printf("%3c",'.');
      }
      printf("\n");
    }
}

void PLAYERMOVE(int board[][COL],int row,int col,int who)
{
    do
    {
      printf("Player[%d], Please play the game.\n",who);
      scanf("%d %d",&x,&y);
      if(x<1 || x>ROW || y<1 ||y>COL)
      {
        printf("You go beyond the boundaries, Please input again.\n");
        continue;
      }
      else if(board[x-1][y-1]!=0)
      {
        printf("This position is occupied, Pease input again.\n");
        continue;
      }
      else
      {
        board[x-1][y-1] = who;
        break;
      }
    }while(1);
}

int Chesscount(int board[][COL],enum dir d)
{   
    int _x = x-1, _y = y-1;
    int count = 0;
    while(1)
    {
    switch(d)
    {
      case LEFT:
        _y--;
        break;
      case RIGHT:
        _y++;
        break;
      case DOWN:
        _x++;
        break;
      case UP:
        _x--;
        break;
      case LEFT_DOWN:
        _x++,_y--;
        break;
      case RIGHT_UP:
        _x--,_y++;
        break;
      case LEFT_UP:
        _x--,_y--;
        break;
      case RIGHT_DOWN:
        _x++,_y++;
        break;
    }
    if(_x<0 || x>COL-1 || _y<0 || _y>ROW-1)
      break;
    else if(board[_x][_y] != board[x-1][y-1])
      break;
    else
      count++;
    }
    return count;
}

int IsOver(int board[][COL],int row, int col)
{
    int count1 = Chesscount(board,LEFT) + Chesscount(board,RIGHT) + 1;  
    int count2 = Chesscount(board,DOWN) + Chesscount(board,UP) + 1;  
    int count3 = Chesscount(board,LEFT_DOWN) + Chesscount(board,RIGHT_UP) + 1;  
    int count4 = Chesscount(board,LEFT_UP) + Chesscount(board,RIGHT_DOWN) + 1;  
    if(count1 >= 5 ||count2 >= 5||count3 >= 5||count4 >= 5)
    {
      if (board[x][y]==PLAYER1)
        return PLAYER1;
      else
        return PLAYER2;
    }
    int i = 0;
    for(i = 0;i<row;i++)
    {
      int j = 0;
      for(j=0;j<col;j++)
      {
        if(board[i][j]==0)
          return NEXT;
      }
    }
    return DREW;
}



void game()
{
  int board[ROW][COL];
  memset(board,0,sizeof(board));
  int result = NEXT;

  do
  { 
    ShowBoard(board,ROW,COL);
    PLAYERMOVE(board,ROW,COL,PLAYER1);
    result = IsOver(board,ROW,COL);
    if(NEXT != result)
    {
      ShowBoard(board,ROW,COL);
      break;
    }

    ShowBoard(board,ROW,COL);
    PLAYERMOVE(board,ROW,COL,PLAYER2);
    result = IsOver(board,ROW,COL);
    if(NEXT != result)
    {
      ShowBoard(board,ROW,COL);
      break;
    }

  }while(1);
  
  switch(result)
  {
    case PLAYER1:
      printf("Player1 win\n");
      break;

    case PLAYER2:
      printf("Player2 win\n");
      break;

    default:
      printf("drew\n");
      break;
  }

}
